<html>
<head><meta charset="utf-8"><title>Subsets that could make sense in `as`? · project-safe-transmute · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/index.html">project-safe-transmute</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F.html">Subsets that could make sense in `as`?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218780470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Subsets%20that%20could%20make%20sense%20in%20%60as%60%3F/near/218780470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F.html#218780470">(Dec 04 2020 at 02:13)</a>:</h4>
<p>One thing that the <code>Scope</code> conversations made me think is that <code>as</code> could be a place where it might seem logical for the language to let you do more with your own types.</p>
<p>I'm imagining things like allowing <code>&amp;T</code>&lt;-&gt;<code>&amp;Wrapper&lt;T&gt;</code> with just <code>#[repr(transparent)]</code> inside the crate in places where you have access to the fields.</p>
<p>I don't know if that's a good idea, or even extends usefully to other transmute checks...</p>



<a name="218852033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Subsets%20that%20could%20make%20sense%20in%20%60as%60%3F/near/218852033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F.html#218852033">(Dec 04 2020 at 16:01)</a>:</h4>
<p>i saw the other discussion where some people made the claim that <code>as</code> isn't explicit enough.</p>
<p>While I mostly don't agree with them, I do at least think that we should avoid making <em>new</em> uses for <code>as</code> if there's any way to not expand this already-overloaded keyword even more.</p>



<a name="218858030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Subsets%20that%20could%20make%20sense%20in%20%60as%60%3F/near/218858030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F.html#218858030">(Dec 04 2020 at 16:43)</a>:</h4>
<p>That's a really neat idea. On a whole, though, <code>as</code> makes me nervous. On some types, it's a bit-reinterpreting conversion (pointer casts, <code>bool -&gt; u8</code>, (un)signed numeric casts), and on others its a logical conversion (numeric casts involving floats), and in at least one case (enum casts) it depends on the layout details of the enum. I've never been able to totally square <code>as</code> in my head with <code>From</code>, either.</p>



<a name="218858485"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Subsets%20that%20could%20make%20sense%20in%20%60as%60%3F/near/218858485" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F.html#218858485">(Dec 04 2020 at 16:46)</a>:</h4>
<p>A quirk of your idea, <span class="user-mention" data-user-id="125270">@scottmcm</span>, is that it <em>might</em> conflict with some enhancements to <code>as</code> <em>other</em> groups are calling for, namely making it a <code>core::op</code> trait. If <code>as</code> permits <code>&amp;T&lt;-&gt;&amp;Wrapper&lt;T&gt;</code> <em>inside</em> the defining crate, <em>and</em> permits overriding with <code>core::op::As</code>, then uses of <code>as</code> <em>inside the defining crate of <code>Wrapper</code></em> potentially become ambiguous.</p>



<a name="218878447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Subsets%20that%20could%20make%20sense%20in%20%60as%60%3F/near/218878447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F.html#218878447">(Dec 04 2020 at 19:12)</a>:</h4>
<p>Well I didn't get into rust until 1.16 but <code>as</code> really reads to me like "we (ergonomically) have to be able to convert numbers easily like C does, but we don't know what the traits for it are supposed to be, so we'll just allow this crazy ad-hoc thing".</p>



<a name="218878657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Subsets%20that%20could%20make%20sense%20in%20%60as%60%3F/near/218878657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F.html#218878657">(Dec 04 2020 at 19:14)</a>:</h4>
<p>AFAIK, the last bastion of <code>as</code>'s necessity is extracting certain enum discriminants. It's the <em>only</em> mechanism for doing this. Unfortunately, it's also a very <em>lossy</em> and <em>incomplete</em> mechanism for doing this. :(</p>



<a name="218880782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Subsets%20that%20could%20make%20sense%20in%20%60as%60%3F/near/218880782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F.html#218880782">(Dec 04 2020 at 19:32)</a>:</h4>
<p>You can use a macro to define the discriminant extracting function, although it feels like the sort of "zero cost abstraction" that might impact the optimizer in an unforeseen way</p>



<a name="218883843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Subsets%20that%20could%20make%20sense%20in%20%60as%60%3F/near/218883843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F.html#218883843">(Dec 04 2020 at 20:00)</a>:</h4>
<p><span class="user-mention silent" data-user-id="219211">Jack Wrenn</span> <a href="#narrow/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F/near/218878657">said</a>:</p>
<blockquote>
<p>AFAIK, the last bastion of <code>as</code>'s necessity is extracting certain enum discriminants. It's the <em>only</em> mechanism for doing this. Unfortunately, it's also a very <em>lossy</em> and <em>incomplete</em> mechanism for doing this. :(</p>
</blockquote>
<p>The library is getting closer to being able to do this -- internally it's now <code>pub struct Discriminant&lt;T&gt;(&lt;T as DiscriminantKind&gt;::Discriminant);</code>, which would be able to provide that value; one just needs to figure out a sane way to do so.</p>
<p>Assuming we got <code>num::WrappingFrom</code>, I could imagine <code>u32::wrapping_from(mem::discriminant(&amp;Ordering::Less))</code>, for example.  I think that would be a perfect match for what <code>as</code> does semantically today.  (Though it's certainly way longer.)</p>
<p>I don't think that's the last one, thought.  A saturating conversion from <code>u128</code> to <code>f32</code> is also only available via <code>as</code>, for example.</p>



<a name="218884292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Subsets%20that%20could%20make%20sense%20in%20%60as%60%3F/near/218884292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F.html#218884292">(Dec 04 2020 at 20:03)</a>:</h4>
<p>I've always been a little surprised there isn't an intrinsic trait in <code>mem</code> like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">Enum</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Discriminant</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">discriminant</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span>::<span class="n">Discriminant</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>...that's automatically implemented for all enums where the discriminant<br>
is well-defined.</p>



<a name="218884372"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Subsets%20that%20could%20make%20sense%20in%20%60as%60%3F/near/218884372" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F.html#218884372">(Dec 04 2020 at 20:03)</a>:</h4>
<p><span class="user-mention silent" data-user-id="224471">Lokathor</span> <a href="#narrow/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F/near/218852033">said</a>:</p>
<blockquote>
<p>i saw the other discussion where some people made the claim that <code>as</code> isn't explicit enough.</p>
<p>While I mostly don't agree with them, I do at least think that we should avoid making <em>new</em> uses for <code>as</code> if there's any way to not expand this already-overloaded keyword even more.</p>
</blockquote>
<p>I'm one of those people who agree that <code>as</code> isn't a good choice for the <em>lossy</em> things it can do, and would like to move those to methods with names describing the intent.</p>
<p>But if those were warned against, I think I'd be ok with more uses of <code>as</code> -- it's the weird kinds of <code>as</code> that make me want to use <code>from</code> instead.</p>



<a name="218892721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Subsets%20that%20could%20make%20sense%20in%20%60as%60%3F/near/218892721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F.html#218892721">(Dec 04 2020 at 21:21)</a>:</h4>
<p>but i only use <code>as</code> in the lossy way</p>



<a name="218892974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Subsets%20that%20could%20make%20sense%20in%20%60as%60%3F/near/218892974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Subsets.20that.20could.20make.20sense.20in.20.60as.60.3F.html#218892974">(Dec 04 2020 at 21:24)</a>:</h4>
<p>My general thought here has been that it's weird that <code>as</code> allows <code>i32 -&gt; u32</code>, but not <code>&amp;i32 -&gt; &amp;u32</code>.  It would be arguably more consistent for it to allow both or neither.  (There's a reasonable discussion about which of those two would be better, but I really can't see anything that makes the reference conversion bad if the value conversion is good.)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>